#include "musicslider.h"
#include "ui_musicslider.h"

MusicSlider::MusicSlider(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MusicSlider)
{
    ui->setupUi(this);
}

MusicSlider::~MusicSlider()
{
    delete ui;
}

void MusicSlider::setProcessIndex()
{
    if(_currentWidth>=this->width())
    {
        _currentWidth = width();
    }
    if(_currentWidth<=0)
    {
        _currentWidth = 0;
    }
    ui->outLine->setGeometry(ui->outLine->x(),ui->outLine->y(),_currentWidth,ui->outLine->height());
}

void MusicSlider::setProcessBarByTime(double ration)
{
    _currentWidth = ration*width();
    setProcessIndex();
}

void MusicSlider::mousePressEvent(QMouseEvent *event)
{
    //当鼠标按下时；
    _currentWidth = event->x();//相对于空间的坐标；
    setProcessIndex();
}
void MusicSlider::mouseReleaseEvent(QMouseEvent *event)
{
    _currentWidth = event->x();
    setProcessIndex();
    emit processBarPosition((double)_currentWidth/width());//发射一个信号给miniQQmusic；
}

void MusicSlider::mouseMoveEvent(QMouseEvent *event)
{
    //获取当前的QRect
    QRect rect(0,0,width(),height());
    if(!rect.contains(event->pos()))
    {
        //不在里面；
        return;
    }
    //当鼠标移动并且点击；
    if(event->buttons() == Qt::LeftButton)
    {
        _currentWidth = event->x();
        setProcessIndex();
    }
}
